<?php

namespace App\Http\Controllers\Api;

use App\BaiduForm;
use App\Events\Customer\CustomerSaved;
use App\Http\Controllers\Controller;
use App\Http\Resources\Customer\CustomerCollection;
use App\Http\Resources\Customer\CustomerResource;
use App\Http\Resources\Customer\SendDepartCollection;
use App\Http\Resources\panyuan\WorkshopCollection;
use App\model\customer\Customer;
use App\model\customer\CustomerArea;
use App\model\customer\CustomerBigDepart;
use App\model\customer\CustomerDetail;
use App\model\customer\CustomerFenpeiRecords;
use App\model\customer\CustomerHangye;
use App\model\customer\CustomerHuanping;
use App\model\customer\CustomerMobile;
use App\model\customer\CustomerPeidian;
use App\model\customer\CustomerRecords;
use App\model\customer\CustomerSecondDepart;
use App\model\customer\CustomerSource;
use App\model\Customer\CustomerWuye;
use App\model\Customer\CustomerXiaofang;
use App\model\Customer\CustomerXuqiu;
use App\model\Workshop;
use App\model\WorkshopIndustry;
use App\Notifications\WechatNotification;
use App\User;
use Auth;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon as SupportCarbon;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;

class CustomerController extends Controller
{
    public function  panyuan(Request $request){
       $customer= Customer::find($request->id);

       $panyuan=Workshop::with('hangye', 'qu', 'industry', 'tags');
       switch($customer->area_id){
           case 1:
            $panyuan=$panyuan->where('area','<',1000);
           break;
           case 2:
            $panyuan=$panyuan->where('area','>=',1000)->where('area','<',2000);
           break;
           case 3:
            $panyuan=$panyuan->where('area','>=',2000)->where('area','<',3000);
           break;
           case 4:
            $panyuan=$panyuan->where('area','>=',3000)->where('area','<',5000);
           break;
           case 5:
            $panyuan=$panyuan->where('area','>=',5000)->where('area','<',8000);
           break;
           case 6:
            $panyuan=$panyuan->where('area','>=',8000)->where('area','<',10000);
           break;
           case 7:
            $panyuan=$panyuan->where('area','>=',10000)->where('area','<',15000);
           break;
           case 8:
            $panyuan=$panyuan->where('area','>=',15000)->where('area','<',20000);
           break;
           case 9:
            $panyuan=$panyuan->where('area','>=',20000)->where('area','<',30000);
           break;
           case 10:
            $panyuan=$panyuan->where('area','>=',30000);
           break;
           default:
            break;
       };
      $industry= WorkshopIndustry::where('name','like','%'.$customer->wuye_format.'%')->get()->pluck('id');
      if(!empty($industry)){
        $panyuan=$panyuan->whereHas('industry', function ($builder) use ($industry) {
            $industry = explode(',', $industry);
            $builder->whereIn('workshop_industry.id', $industry);
        });
      }
      $panyuan=$panyuan->take(3)->get();
       return new \App\Http\Resources\Customer\WorkshopCollection($panyuan);
    }

    public function bigpart(){
        $res=CustomerBigDepart::get(['id','name']);
        return $this->responsed(200,$res);
    }
    /**
     * 物业列表
     */
    public function custWuye(Request $request){
        $res = CustomerWuye::orderBy('sort','asc')->get(['wuye_id as id','name']);
        return $this->responsed(200,$res);
    }

    /**
     * 行业列表
     */
    public function custHangye(Request $request){
        $res = CustomerHangye::orderBy('sort','asc')->get(['hangye_id as id','name']);
        return $this->responsed(200,$res);
    }

    /**
     * 面积列表
     */
    public function custArea(Request $request){
        $res = CustomerArea::orderBy('sort','asc')->get(['area_id as id','name']);
        return $this->responsed(200,$res);
    }

    /**
     * 需求列表
     */
    public function custXuqiu(Request $request){
        $res = CustomerXuqiu::orderBy('sort','asc')->get(['xuqiu_id as id','name']);
        return $this->responsed(200,$res);
    }

    /**
     * 配电列表
     */
    public function custPeidian(Request $request){
        $res = CustomerPeidian::orderBy('sort','asc')->get(['peidian_id as id','name']);
        return $this->responsed(200,$res);
    }

    /**
     * 消防列表
     */
    public function custXiaofang(Request $request){
        $res = CustomerXiaofang::orderBy('sort','asc')->get(['xiaofang_id as id','name']);
        return $this->responsed(200,$res);
    }

    /**
     * 环评列表
     */
    public function custHuanping(Request $request){
        $res = CustomerHuanping::orderBy('sort','asc')->get(['huanping_id as id','name']);
        return $this->responsed(200,$res);
    }

    /**
     * 资源列表
     */
    public function custSource(Request $request){
        $res = CustomerSource::orderBy('sort','asc')->get(['source_id as id','name']);
        return $this->responsed(200,$res);
    }
    /**
     * 客户编辑
     */
    public function update(Request $request){
        $valid = Validator::make($request->all(), [
            'name' => 'required|string',
            'telephone' => 'required',
            'address' => 'required|string|min:2',
            'create_owner_name'=>'required',
            'big_part_id'=>'required',
        ]);
        if ($valid->fails()) {
            return array(
                'code' => 201,
                'res' => $valid->errors()->first(),
            );
        }
        $data = request(['name','telephone','address','area_id','hangye_id','peidian_id',
            'xuqiu_id','source_id','huanping_id','wuye_id','xiaofang_id','desc','create_owner_name','big_part_id','group_id']);

        $customer= Customer::find($request->id);
        $customer->name=$data['name'];
        $customer->telephone=$data['telephone'][0];
        $customer->address=$data['address'];
        $customer->area_id=$data['area_id'];
        $customer->hangye_id=$data['hangye_id'];
        $customer->peidian_id=$data['peidian_id'];

        $customer->xuqiu_id=$data['xuqiu_id'];
        $customer->source_id=$data['source_id'];
        $customer->huanping_id=$data['huanping_id'];
        $customer->wuye_id=$data['wuye_id'];
        $customer->xiaofang_id=$data['xiaofang_id'];
       // $customer->create_user_name=$data['create_owner_name'];
        $customer->big_part_id=$data['big_part_id'];

        $customer->desc=$data['desc'];
        $res = $customer->save();
        CustomerMobile::whereCustomerId($request->id)->delete();
        foreach($data['telephone'] as $item){
            $customerMobile= new CustomerMobile();
            $customerMobile->mobile=$item;
            $customerMobile->customer_id=$customer->customer_id;
            $customerMobile->save();
        }
         event(new  CustomerSaved($customer));

        return $this->responsed(200,$res);
    }
    public function customerCreateSendMessage(Request $request,$id){
        $customer=Customer::find($id);
        if(!$customer){
            return $this->responsed(200,[
                'code'=>'error',
                'message'=>'没有此客户'
            ]);
        }
     //推送
     $users = User::permission('allot_group')
     ->get();
    if ($users->isNotEmpty()){
     foreach ($users as $item){
         $item->notify(new WechatNotification($customer));
     }
    }
    return $this->responsed(200,[
        'code'=>'success',
        'message'=>'发送成功'
    ]);
    }
    public function distributionSendMessage(Request $request,$id){

        $customer=Customer::find($id);

        if(!$customer){
            return $this->responsed(200,[
                'code'=>'error',
                'message'=>'没有此客户'
            ]);
        }
        $group_id=$customer->group_id;
        if(!$group_id){
            return $this->responsed(200,[
                'code'=>'error',
                'message'=>'没有分配到组'
            ]);
        }

        $users = User::permission('allot_user')
        ->whereHas('groups',function ($query)use ($group_id){
            $query->where('customer_second_depart.id','=',$group_id);
        })
        ->get();

    if ($users->isNotEmpty()){
        foreach ($users as $item){
            $item->notify(new WechatNotification($customer));
        }
    }
    return $this->responsed(200,[
        'code'=>'success',
        'message'=>'发送成功'
    ]);
    }
    public function memberSendMessage(Request $request,$id){
        $customer=Customer::find($id);
        if(!$customer){
            return $this->responsed(200,[
                'code'=>'error',
                'message'=>'没有此客户'
            ]);
        }
        $ower_user_id=$customer->ower_user_id;
        if(!$ower_user_id){
            return $this->responsed(200,[
                'code'=>'error',
                'message'=>'没有分配到人'
            ]);
        }
              //推送
              User::find($ower_user_id)->notify(new WechatNotification($customer));
              return $this->responsed(200,[
                'code'=>'success',
                'message'=>'发送成功'
            ]);
    }
    public function sendMessage(Request $request,$id){
        $customer=Customer::find($id);
        $title=$request->title;
        $desc=$request->desc;
        $user_id=$request->user_id;
        //推送
        User::find($user_id)->notify(new WechatNotification($customer,$title,$desc));
        return $this->responsed(200,[
            'code'=>'success',
            'message'=>'发送成功'
        ]);
    }
    /**
     * 客户录入(提交)
     */
    public function submitCustomer(Request $request){
        $user=auth('api')->user();
       if (!$user->hasPermissionTo('add_customer')){
           return [
               'code' => 201,
               'message' => '无权限操作',
           ];
       }
        $valid = Validator::make($request->all(), [
            'name' => 'required|string',
            'telephone' => 'required',
            'address' => 'required|string|min:2',
            'create_owner_name'=>'required',
            'big_part_id'=>'required',
        ]);
        if ($valid->fails()) {
            return array(
                'code' => 201,
                'res' => $valid->errors()->first(),
            );
        }

        $data = request(['name','telephone','address','area_id','hangye_id','peidian_id',
            'xuqiu_id','source_id','huanping_id','wuye_id','xiaofang_id','desc','create_owner_name','big_part_id','group_id']);
        $user = auth('api')->user();
        $data['create_user_id'] = $user->id;

        $customer= new Customer();

        $customer->name=$data['name'];
        $customer->telephone=$data['telephone'][0];
        $customer->address=$data['address'];
        $customer->area_id=$data['area_id'];
        $customer->hangye_id=$data['hangye_id'];
        $customer->create_user_id=$user->id;
        $customer->peidian_id=$data['peidian_id'];
        $customer->xuqiu_id=$data['xuqiu_id'];
        $customer->source_id=$data['source_id'];
        $customer->huanping_id=$data['huanping_id'];
        $customer->wuye_id=$data['wuye_id'];
        $customer->xiaofang_id=$data['xiaofang_id'];
        $customer->create_user_name=$data['create_owner_name'];
        $customer->big_part_id=$data['big_part_id'];

        $customer->record_time=Carbon::now();
        if ($data['group_id']){
            $customer->group_id=$data['group_id'];
            $customer->status=Customer::FOLLOW_TO_GROUP;
        }
        $customer->desc=$data['desc'];
        $res = $customer->save();
        foreach($data['telephone'] as $item){
            $customerMobile= new CustomerMobile();
            $customerMobile->mobile=$item;
            $customerMobile->customer_id=$customer->customer_id;
            $customerMobile->save();
        }

        event(new  CustomerSaved($customer));
        //推送
        $users = User::permission('allot_group')
            ->where('id','!=',$user->id)
            ->get();
        if ($users->isNotEmpty()){
            foreach ($users as $item){
                $item->notify(new WechatNotification($customer));
            }
        }
        return $this->responsed(200,$res);
    }
    public function submitStatus(Request $request){
        $customer_id=$request->customer_id;
        $follow_status_id=$request->follow_status_id;
        $customer=Customer::find($customer_id);
        $customer->follow_status=$follow_status_id;
        $customer->save();
        return $this->responsed(200,$customer);
    }
    /**
     * 获取客户信息列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCustomers(Request $request){

        $user = auth('api')->user();

        $customer = Customer::with(['area']);


        if (isset($request['group_id'])&&$request['group_id']!=''){
            $customer = $customer->where('group_id',$request['group_id']);
        }
       if($request['status']!=null&&$request['status']!=''){
            $customer = $customer->where('status',$request['status']);
        }
        if($request['status']==2&&!empty(json_decode($request['userIds']))){
            $customer = $customer->whereIn('ower_user_id',json_decode($request['userIds']));

        }
        if($request['status']==2){
            $customer=$customer->orderBy('follow_status','asc');
        }
        if($request['follow_status']!=null&&$request['follow_status']!=''){
        $customer = $customer->where('follow_status',$request['follow_status']);
        }
       if ($user->auth_map==User::VIEW_CHILDREN){//组数据
          $groupIds= $user->groups->pluck('id');
          $big_groups=$user->big_groups->pluck('id');
           $customer = $customer->whereIn('group_id',$groupIds);
       }
       if ($user->auth_map==User::VIEW_ONE){
           $customer = $customer->where('ower_user_id',$user->id);
       }/*else{
           //看到自己创建的
           $customer=$customer->orWhere('create_user_id',$user->id);
       }*/
    /*    if (!$user->hasPermissionTo('allot_group')){//没有分配到组的权限看不到这个数据
            $customer=$customer->where('status','!=',Customer::NO_FOLLOW);
        }*/
        if(isset($request['timeArrow'])&&$request['timeArrow']=='false'||!isset($request['timeArrow'])){
            $customer=$customer->orderBy('created_at','desc');
        }else{
            $customer=$customer->orderBy('created_at','asc');
        }
        $customer = $customer->paginate(10);

        return new CustomerCollection($customer);
    }

    /**
     * 获取客户详细信息及跟踪记录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCustomerDetails(Request $request){
        $id = $request['customer_id'];
        $customer = Customer::with('depart','records','owner','bigDepart','mobiles')->find($id);

        return new CustomerResource($customer);
    }
    /**
     * 客户分配到组
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     */
    public function shareGroupCustomer(Request $request)
    {
        $id = $request['customer_id'];
        $group_id = $request['group_id'];
        if (!$group_id||!$id){
            return response()->json(['code' => 201, 'res' => '参数错误！']);
        }
        $customer = Customer::where('customer_id', $id)->where('status', 0)->first();
        if ($customer == null) {
            return response()->json(['code' => 201, 'res' => '该客户已分配到组！']);
        } else {
            \DB::transaction(function () use ($customer, $group_id, $id) {
                $customer->group_id = $group_id;
                $customerDepart=CustomerSecondDepart::find($group_id);
                $customerDepart->number=$customerDepart->number+1;
                $customerDepart->save();
                $customer->status = 1;
                $res = $customer->save();
                $customerFenpeiRecord = new CustomerFenpeiRecords;
                $customerFenpeiRecord->customer_id = $id;
                $user = auth('api')->user();
                $customerFenpeiRecord->option_user_id = $user->id;
                $customerFenpeiRecord->group_id = $group_id;
                $customerFenpeiRecord->save();
            });
            //推送
            $users = User::permission('allot_user')
                ->where('id','!=',auth('api')->id())
                ->whereHas('groups',function ($query)use ($group_id){
                    $query->where('customer_second_depart.id','=',$group_id);
                })
                ->get();
            if ($users->isNotEmpty()){
                foreach ($users as $item){
                    $item->notify(new WechatNotification($customer));
                }
            }
            return response()->json(['code' => 200, 'res' => '分配小组成功！']);
        }
    }
    public function getUsers(Request $request){
        $customer=$request->customer_id;
        $customer=Customer::find($customer);

        $customer=CustomerSecondDepart::with('children','parent')->find($customer->group_id);
        return response()->json(['code' => 200, 'data' =>[$customer] ]);
    }
    public function getSecondDepartUser(Request $request){
        $groupIds=$request->groupIds;
        $customer=CustomerSecondDepart::with(['children','parent'])->whereIn('id', json_decode($groupIds))->get();
        return response()->json(['code' => 200, 'data' =>$customer ]);
    }
    /**
     * 自己领取
     */
    public function shareUserMe(Request $request){
        $id=$request->input('customer_id');
        $ower_user_id = auth('api')->id();
        $customer = Customer::where('customer_id',$id)->where('status',1)->first();
        if($customer == null){
            return response()->json(['code'=>201,'res'=>'该客户已分配人员！']);
        }else{
            \DB::transaction(function ()use ($customer,$ower_user_id,$id) {
                $customer-> ower_user_id= $ower_user_id;
                $customer->status = 2;
                $res = $customer->save();
                $customerFenpeiRecord = new CustomerFenpeiRecords;
                $customerFenpeiRecord->customer_id = $id;
                $user = User::find(auth('api')->id());
                $user->number= $user->number+1;
                $user->save();
                $customerFenpeiRecord->option_user_id = $user->id;
                $customerFenpeiRecord->user_id = $ower_user_id;
                $customerFenpeiRecord->save();
            });
            return response()->json(['code'=>200,'res'=>'分配人员成功！']);
        }
    }
    /**
     * 客户分配
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     */
    public function shareUserCustomer(Request $request){
        $id = $request['customer_id'];
        $ower_user_id = $request['ower_user_id'];
        $customer = Customer::where('customer_id',$id)/*->where('status',1)*/->first();

        if($customer == null/*&&!\Illuminate\Support\Carbon::parse($customer->record_time)->addDays(-7)->gt(Carbon::now())*/){
            return response()->json(['code'=>201,'res'=>'该客户已分配人员！']);
        }else{
            \DB::transaction(function ()use ($customer,$ower_user_id,$id) {
                $customer-> ower_user_id= $ower_user_id;
                $customer->status = 2;
                $customer->record_time=Carbon::now();
                $res = $customer->save();

                $customerFenpeiRecord = new CustomerFenpeiRecords;
                $customerFenpeiRecord->customer_id = $id;
                $user = auth('api')->user();
                $customerFenpeiRecord->option_user_id = $user->id;
                $customerFenpeiRecord->user_id = $ower_user_id;
                $customerFenpeiRecord->save();
            });
            //推送
            User::find($ower_user_id)->notify(new WechatNotification($customer));
            return response()->json(['code'=>200,'res'=>'分配人员成功！']);
        }
        //推送
    }
        /**
     * 客户重新分配
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     */
    public function repeatShareUserCustomer(Request $request){
        $id = $request['customer_id'];
        $ower_user_id = $request['ower_user_id'];
        if(!$ower_user_id){
            return response()->json(['code'=>201,'res'=>'请选择分配的人员']);
        }

        $customer = Customer::where('customer_id',$id)->first();
        $user= User::find($ower_user_id);
        if($customer == null/*&&!\Illuminate\Support\Carbon::parse($customer->record_time)->addDays(-7)->gt(Carbon::now())*/){
            return response()->json(['code'=>201,'res'=>'该客户已分配人员！']);
        }else{
            \DB::transaction(function ()use ($customer,$ower_user_id,$id,$user) {
                $customer-> ower_user_id= $ower_user_id;
                $customer->follow_status=2;
                $customer->is_chongfen=0;
                $customer->group_id=$user->department_id;
                $customer->big_part_id=CustomerSecondDepart::find($user->department_id)->parent_id;
                $customer->status = 2;
                $customer->created_at=Carbon::now();
                $customer->record_time=Carbon::now();
                $res = $customer->save();
                $customerFenpeiRecord = new CustomerFenpeiRecords;
                $customerFenpeiRecord->customer_id = $id;
                $user = auth('api')->user();
                $customerFenpeiRecord->option_user_id = $user->id;
                $customerFenpeiRecord->user_id = $ower_user_id;
                $customerFenpeiRecord->save();
            });
            //推送
            User::find($ower_user_id)->notify(new WechatNotification($customer));
            return response()->json(['code'=>200,'res'=>'分配人员成功！']);
        }
        //推送
    }

    /**
     * 编辑跟踪记录
     */
    public function followUpdate(Request $request){
        $follow_id=$request->follow_id;
        $desc = $request->desc;
        $customerRecords = CustomerRecords::find($follow_id);

        if($desc){
            $customerRecords->desc=$desc;
        }
       $customerRecords->save();
        return response()->json(['code'=>200,'res'=>'成功']);
    }
    /**
     * 编写跟踪记录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setCustomerRecords(Request $request){
        $valid = Validator::make($request->all(), [
            'customer_id' => 'required',
            'desc' => 'required|string|min:2',
        ]);

        if ($valid->fails()) {
            return array(
                'code' => 201,
                'message' => $valid->errors()->first(),
            );
        }
        $id = $request['customer_id'];
        $desc = $request['desc'];
        $customer=Customer::find($id);
        $customer->record_time=Carbon::now();
        $customer->is_chongfen=0;
        $customer->save();
        $customerRecords = new CustomerRecords;
        $customerRecords->customer_id = $id;
        $customerRecords->desc = $desc;
        $res = $customerRecords->save();
        return response()->json(['code'=>200,'res'=>$res]);
    }

    /**
     * 容量统计
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function sourceCount(Request $request)
    {
        $dates = Carbon::today()->toDateString();
        $month = substr($dates,0,7);
        $res = CustomerSource::orderBy('sort','asc')->get(['source_id as id','name','image']);
        foreach ($res as $re){
            $num = Customer::where('source_id',$re->id)->where('created_at','like',$dates.'%')->count();
            $m_num = Customer::where('source_id',$re->id)->where('created_at','like',$month.'%')->count();
            $re['today_num'] = $num;
            $re['month_num'] = $m_num;
            $re['image']=Storage::url($re['image']);
        }
        return response()->json(['code'=>200,'res'=>$res]);
    }

    public function getDepartments(Request $request){
        $user=auth('api')->user();
        $customer_id=$request->customer_id;

        if ($customer_id){
            $customer=Customer::find($customer_id);
            $datas = CustomerBigDepart::where('id',$customer->big_part_id)->get(['id','name']);
            if ($datas->isEmpty()){
                $datas = CustomerBigDepart::get(['id','name']);
            }
        }else{
            if ($user->auth_map==0){
                $datas = CustomerBigDepart::get(['id','name']);
            }elseif ($user->auth_map==2){
                $bigDepart=$user->big_groups->pluck('id');
                $datas = CustomerBigDepart::whereIn('id',$bigDepart)->get(['id','name']);
            }

        }
        foreach ($datas as $data){
            if ($user->auth_map==0){
                $seconds = CustomerSecondDepart::where('parent_id',$data->id);
            }elseif ($user->auth_map==2){
                $secondDepart=$user->groups->pluck('id');
                $seconds = CustomerSecondDepart::whereIn('id',$secondDepart)->where('parent_id',$data->id);
            }
            if (!empty($groups)){
                $seconds = $seconds->whereIn('id',$groups);
            }
            $seconds = $seconds->get(['id','name','number']);
       /*     foreach ($seconds as $second){
                $second['children'] = User::where('department_id',$second->id)->get(['id','name']);
            }*/

            $data['children'] = $seconds;
        }
        return response()->json(['code'=>200,'res'=>$datas]);
    }

    public function baiduForm(Request $request)
    {
        $project_id = $request->big_part_id ?? 2;
        $data = json_decode($request->getContent(), true) ?? [];
        $create_user_id = $request->create_user_id ?? 1;
        \Log::info('百度form', $data);
        $baiduForm = new BaiduForm();
        $baiduForm->form_data = json_encode($data['data']);
        $baiduForm->site_id = $data['site_id'];
        $baiduForm->page_id = $data['page_id'];
        $baiduForm->save();

        $name = collect($data['data'])->firstWhere('name', '=', '姓名')['value'];
        $mobile = collect($data['data'])->firstWhere('name', '=', '电话')['value'];

        $lead = Customer::whereHas('mobiles', function ($query)use ($mobile){
            $query->where('customer_mobile',$mobile);
        })->where('big_part_id', '=', $project_id)->first();
        if ($lead) {
            return 'success';
        }
        $lead = new Customer();
        $lead->customer_id = 0;

        if ($name) {
            $lead->name = $name;
        }
        if ($mobile) {
            $lead->telephone = $mobile;
        }
        $lead->source_format = '百度';
        $lead->save();
        $customerMobile= new CustomerMobile();
        $customerMobile->mobile=$mobile;
        $customerMobile->customer_id=$lead->customer_id;
        $customerMobile->save();
        event(new CustomerSaved($lead) );
        \Log::info('新增线索', $lead->toArray());
        $users = User::permission('allot_group')
            ->get();
        if ($users->isNotEmpty()){
            foreach ($users as $item){
                $item->notify(new   WechatNotification($lead));
            }
        }
        return 'success';
    }
    private function responsed($code,$data){
        return response()->json(['code'=>$code,'res'=>$data]);
    }
    public function group(Request $request){
        $bigPart=$request->big_part_id;
/*        $user=auth('api')->user();
        if ($user->auth_map==User::VIEW_ALL){
            $groups=[];
        }
        if ($user->auth_map==User::VIEW_CHILDREN){
            $groups=$user->groups->pluck('id');
        }
        if ($user->auth_map==User::VIEW_ONE){
            $groups=[$user->department_id];
        }*/
        $part= CustomerSecondDepart::where('parent_id',$bigPart)->get(['id','name']);
        return $this->responsed(200,$part);
    }
    /**
     * 分组人员
     * depart_id
     */
    public function  groupUser(){
         $depart=CustomerSecondDepart::with('user')->get();
         return  new SendDepartCollection($depart);
    }
}
